package com.lianbang.pharmacy.adapter

import android.widget.ImageView
import cn.ubest.mvvmcore.utils.GlideUtils
import cn.ubest.mvvmcore.utils.ResourcesUtils
import cn.ubest.mvvmcore.utils.SpanUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.lianbang.pharmacy.R
import com.lianbang.pharmacy.bean.MedicineListBean

/**
 * Created by maoqi on 2020/11/23.
 */
class MedicineListAdapter :
    BaseQuickAdapter<MedicineListBean, BaseViewHolder>(R.layout.item_medicine_list) {
    override fun convert(helper: BaseViewHolder, item: MedicineListBean) {
        GlideUtils.display(context, item.image, helper.getView<ImageView>(R.id.iv_img))

        val price = item.price.trim()
        if (price.isNotEmpty()) {
            val spanUtils = SpanUtils()
            if (price.contains(".")) {
                //是否包含小数点
                val split = price.split(".")
                val integerPlace = split[0]
                spanUtils.append("¥")
                    .setFontSize(ResourcesUtils.getDimens(R.dimen.sp_14))
                    .append(integerPlace)
                    .setFontSize(ResourcesUtils.getDimens(R.dimen.sp_18))
                if (split.size > 1) {
                    val decimalPlace = split[1]
                    spanUtils.append(".")
                        .setFontSize(ResourcesUtils.getDimens(R.dimen.sp_16))
                        .append("${decimalPlace}起")
                        .setFontSize(ResourcesUtils.getDimens(R.dimen.sp_14))
                }
            } else {
                spanUtils.append("¥")
                    .setFontSize(ResourcesUtils.getDimens(R.dimen.sp_14))
                    .append("${price}起")
                    .setFontSize(ResourcesUtils.getDimens(R.dimen.sp_18))
            }

            helper.setText(
                R.id.tv_price, spanUtils.create()
            )
        }

        helper
            .setText(R.id.tv_title, item.store_name)
            .setText(R.id.tv_tag, "颗粒剂")
            .setText(R.id.tv_standard, "10gx9袋/盒")
            .setText(R.id.tv_content, "国药标准字Z44021940\n华润三九医药股份有限公司")
            .setText(
                R.id.tv_supplier, SpanUtils()
                    .append("5")
                    .setForegroundColor(ResourcesUtils.getColor("#5671FB"))
                    .append("家供应商在售")
                    .create()
            )

    }

}